package com.atmilan.quartz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atmilan.quartz.mapper.BizManifestMapper;
import com.atmilan.quartz.mapper.BizManifestProductSkuSupplierMapper;
import com.atmilan.quartz.mapper.ManifestMapper;
import com.atmilan.quartz.mapper.ManifestProductMapper;
import com.atmilan.quartz.mapper.ManifestProductSkuMapper;
import com.atmilan.quartz.mapper.ManifestProductSkuSupplierMapper;
import com.atmilan.quartz.model.dto.buyer.ManifestDTO;
import com.atmilan.quartz.model.entity.buyer.Manifest;
import com.atmilan.quartz.model.entity.buyer.ManifestExample;
import com.atmilan.quartz.model.entity.buyer.ManifestProductExample;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSkuExample;
import com.atmilan.quartz.model.entity.buyer.ManifestProductSkuSupplierExample;
import com.atmilan.quartz.model.vo.buyer.ManifestQuery;
import com.atmilan.quartz.service.IManifestService;
import com.atmilan.quartz.utils.BeanConvert;
import com.atmilan.quartz.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.db.PageResult;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author muyu
 * @date 2020-09-01
 */
@Service
public class ManifestServiceImpl implements IManifestService {
    @Autowired
    private ManifestMapper manifestMapper;
    @Autowired
    private BizManifestMapper bizManifestMapper;
    @Autowired
    ManifestProductMapper manifestProductMapper;
    @Autowired
    ManifestProductSkuSupplierMapper manifestProductSkuSupplierMapper;
    @Autowired
    ManifestProductSkuMapper manifestProductSkuMapper;
    @Autowired
    BizManifestProductSkuSupplierMapper bizManifestProductSkuSupplierMapper;
    
    @Override
    public List<ManifestDTO> getManifestByMerchantId(Long merchantId) {

        return BeanConvert.copyBeanList(bizManifestMapper.getManifestByMerchantId(merchantId), ManifestDTO.class);
    }

    @Override
    public Manifest getByManifestNameAndMerchantId(String manifestName, Long merchantId) {
        ManifestExample manifestExample = new ManifestExample();
        manifestExample.createCriteria().andManifestNameEqualTo(manifestName).andMerchantIdEqualTo(merchantId);
        List<Manifest> list = manifestMapper.selectByExample(manifestExample);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Manifest getByManifestName(String manifestName) {
        ManifestExample manifestExample = new ManifestExample();
        manifestExample.createCriteria().andManifestNameEqualTo(manifestName);
        List<Manifest> list = manifestMapper.selectByExample(manifestExample);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public ManifestDTO getManifestById(Long id) {
        return BeanConvert.copyBean(manifestMapper.selectByPrimaryKey(id), ManifestDTO.class);
    }


    /**
     * 查询【请填写功能名称】列表
     * 
     * @param manifest 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public PageResult<ManifestDTO> queryManifestList(ManifestQuery manifest) {
        Page page = PageHelper.startPage(manifest.getPageNo(), manifest.getLimit());
        List<Manifest> doList = bizManifestMapper.queryPageList(manifest);
        List<ManifestDTO> resList = BeanConvert.copyBeanList(doList, ManifestDTO.class);
        for (ManifestDTO manifestDTO : resList) {
            ManifestProductExample manifestProductExample = new ManifestProductExample();

            manifestProductExample.createCriteria().andManifestIdEqualTo(manifestDTO.getId());

            ManifestProductSkuExample manifestProductSkuExample = new ManifestProductSkuExample();

            manifestProductSkuExample.createCriteria().andManifestIdEqualTo(manifestDTO.getId());

            manifestDTO.setSkuCount(manifestProductSkuMapper.countByExample(manifestProductSkuExample));
            manifestDTO.setProductCount(manifestProductMapper.countByExample(manifestProductExample));

            Integer stockCount = bizManifestProductSkuSupplierMapper.getStockCountByManifestId(manifestDTO.getId());
            manifestDTO.setStockCount(stockCount);

        }
        return PageUtils.buildPage(page, resList);

    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param manifest 【请填写功能名称】
     * @return 结果
     */
    @Override
    public Long insertManifest(ManifestDTO manifest) {
        manifest.setCreateTime(new Date());
        manifest.setUpdateTime(new Date());
        Manifest manifest1 = BeanConvert.copyBean(manifest, Manifest.class);
        manifestMapper.insert(manifest1);
        return manifest1.getId();
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param manifest 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean updateManifest(ManifestDTO manifest) {

        return manifestMapper.updateByPrimaryKeySelective(BeanConvert.copyBean(manifest, Manifest.class)) > 0;

    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public boolean deleteManifestByIds(String ids) {
        return true;
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteManifestById(Long id) {

        ManifestProductExample manifestProductExample = new ManifestProductExample();
        manifestProductExample.createCriteria().andManifestIdEqualTo(id);
        manifestProductMapper.deleteByExample(manifestProductExample);

        ManifestProductSkuSupplierExample manifestProductSkuSupplierExample = new ManifestProductSkuSupplierExample();
        manifestProductSkuSupplierExample.createCriteria().andManifestIdEqualTo(id);
        manifestProductSkuSupplierMapper.deleteByExample(manifestProductSkuSupplierExample);

        ManifestProductSkuExample manifestProductSkuExample = new ManifestProductSkuExample();
        manifestProductSkuExample.createCriteria().andManifestIdEqualTo(id);
        manifestProductSkuMapper.deleteByExample(manifestProductSkuExample);

        return manifestMapper.deleteByPrimaryKey(id) > 0;
    }

}
